/**
 *	Copyright (c) 2009, SpLib.net
 * 	All rights reserved.
  	
 * 	Author:6spring
 * 	About:调试信息输出
*/

package SpLib.Tools
{
	import SpLib.Net.Socket;
	import SpLib.Com.SingletonFactory;
	
	public class Tr
	{
		public static const TRACE_ERROR:int = 0;
		public static const TRACE_SYSNOTE:int = 1;
		public static const TRACE_WARNING:int = 2;
		public static const TRACE_INFO:int = 3;
		public static const TRACE_NETIN:int = 4;
		public static const TRACE_NETOUT:int = 5;
		public static const TRACE_CALLSVR:int = 6;
		public static const TRACE_CALLSVRET:int = 7;
		
		private static var s_Instance:Tr = null;
		private static var m_LocTraceFunc:Function;
		
		private var m_Link:SpLib.Net.Socket = new SpLib.Net.Socket(true);
			
		public static function GetInstance():Tr
		{
			var iname:String = "SpLib.Tools.Tr";
			if(s_Instance==null)
			{
				s_Instance = SingletonFactory.Get(iname) as SpLib.Tools.Tr;
				if(s_Instance==null)
				{			
					s_Instance = new SpLib.Tools.Tr();
					SingletonFactory.Reg(iname,s_Instance);
				}
			}
						
			return s_Instance;
		}
		
		public function Tr()
		{
			if(SpLib.Tools.Tr.s_Instance!=null)
			{
				//throw new Error("Tr is Singleton");
			}
			
			if(SingletonFactory.Get("SpLib.Tools.Tr")!=null)
			{
				//throw new Error("Tr is Singleton");
			}
			
			m_Link.tracesocket = true;
			m_Link.Connect("127.0.0.1",9438);
		}
		
		public static function SetTraceFunc(func:Function):void
		{
			m_LocTraceFunc = func;
		}

		public static function Error(msg:String):void
		{
			GetInstance().Show(TRACE_ERROR,msg);
		}
		
		public static function SysNote(msg:String):void
		{
			GetInstance().Show(TRACE_SYSNOTE,msg);
		}
		
		public static function Warning(msg:String):void
		{
			GetInstance().Show(TRACE_WARNING,msg);
		}
		
		public static function Info(msg:String):void
		{
			GetInstance().Show(TRACE_INFO,msg);
		}
		
		public static function ToC(msg:String):void
		{
			GetInstance().Show(TRACE_NETIN,msg);
		}
		
		public static function ToS(msg:String):void
		{
			GetInstance().Show(TRACE_NETOUT,msg);
		}
		
		public static function Call(msg:String):void
		{
			GetInstance().Show(TRACE_CALLSVR,msg);
		}
		
		public static function CallRet(msg:String):void
		{
			GetInstance().Show(TRACE_CALLSVRET,msg);
		}
		
		public function Show(type:int,msg:String):void
		{
			if(m_Link.connected)
			{
				var pak:SpLib.Net.NetPak = new SpLib.Net.NetPak;
				pak.m_Msg = new SpLib.Net.NetMsg;
				pak.m_Msg.m_nMsgtype = SpLib.Net.NetMsg.TYPE_TRACE;
				pak.m_Msg.m_nEveID = type;
				pak.m_Msg.m_Value.push(msg);
				m_Link.Send(pak);
			}
			
			if(m_LocTraceFunc!=null)
			{
				m_LocTraceFunc(type,msg);
			}
		}
	}
}